package com.itmei.exceldemo.utils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author itmei
 * @Date 2024/4/13 21:44
 * @description: HttpServlet工具类用于获取 Request，Response
 * @Title: HttpServletUtil
 * @Package com.itmei.exceldemo.utils
 */
public class HttpServletUtil {

   public static HttpServletRequest getRequest() {
      ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
      if (requestAttributes == null) {
         throw new RuntimeException("当前请求参数为空或数据缺失");
      } else {
         return requestAttributes.getRequest();
      }
   }

   public static HttpServletResponse getResponse() {
      ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
      if (requestAttributes == null) {
         throw new RuntimeException("当前请求参数为空或数据缺失");
      } else {
         return requestAttributes.getResponse();
      }
   }
}
